package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;

import com.itheima.constant.MessageConstant;
import com.itheima.domain.*;
import com.itheima.service.Role_managementService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/role")
@ResponseBody
public class Role_managementController {
    @Reference
    private Role_managementService role_managementService;

    @RequestMapping("/findAll")
    public List<Role_management> findAll(){
        return role_managementService.findAll();
    }

    @RequestMapping("/EstimateFindAll")
    public List<Role_management> EstimateFindAll(){
        return role_managementService.EstimateFindAll();
    }

    @RequestMapping("/save")
    public void save(@RequestBody Role_management role_management){
        role_managementService.save(role_management);
    }

    //删除
    @RequestMapping("/delete")
    public Result delete(Integer id){
        try {
            role_managementService.delete(id);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }catch (Exception e){
            return new Result(false, MessageConstant.DELETE_CHECKITEM_FAIL);
        }return new Result(true,MessageConstant.DELETE_CHECKITEM_SUCCESS);
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = role_managementService.pageQuery(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString());
        return pageResult;
    }


    //编辑
    @RequestMapping("/edit")
    public Result edit(@RequestBody Role_management role_management){
        try {
            role_managementService.edit(role_management);
        }catch (Exception e){
            return new Result(false,MessageConstant.EDIT_CHECKITEM_FAIL);
        }
        return new Result(true,MessageConstant.EDIT_CHECKITEM_SUCCESS);
    }
    @RequestMapping("/findById")
    public Role_management findById(Integer id){
//        try{
        Role_management role_management = role_managementService.findById(id);
        return role_management;
//            return new Result(true,
//                    MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem);
//        }catch (Exception e){
//            e.printStackTrace();
//
//            return new Result(false, MessageConstant.QUERY_CHECKITEM_FAIL);
//        }
    }


//    @RequestMapping("/findById")
//    public Role_management findById(Integer id){
//        return role_managementService.findById(id);
//    }
    @RequestMapping("/updateRole")
    public void updatePermission(Role_management role_management){
        role_managementService.updateRole(role_management);
    }
}
